package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xjy.common.utils.PageUtils;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.system.domain.bo.SysCycleAddBo;
import com.xjy.system.domain.bo.SysCycleQueryBo;
import com.xjy.system.domain.bo.SysCycleEditBo;
import com.xjy.system.domain.SysCycle;
import com.xjy.system.mapper.SysCycleMapper;
import com.xjy.system.domain.vo.SysCycleVo;
import com.xjy.system.service.ISysCycleService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 收款周期Service业务层处理
 *
 * @author zc
 * @date 2022-03-08
 */
@Service
public class SysCycleServiceImpl extends ServicePlusImpl<SysCycleMapper, SysCycle> implements ISysCycleService {

	@Override
	public SysCycleVo queryById(Long id) {
		return getVoById(id, SysCycleVo.class);
	}

	@Override
	public TableDataInfo<SysCycleVo> queryPageList(SysCycleQueryBo bo) {
		PagePlus<SysCycle, SysCycleVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysCycleVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<SysCycleVo> queryList(SysCycleQueryBo bo) {
		return listVo(buildQueryWrapper(bo), SysCycleVo.class);
	}

	private LambdaQueryWrapper<SysCycle> buildQueryWrapper(SysCycleQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<SysCycle> lqw = Wrappers.lambdaQuery();
		lqw.eq(StrUtil.isNotBlank(bo.getCycle()), SysCycle::getCycle, bo.getCycle());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(SysCycleAddBo bo) {
		SysCycle add = BeanUtil.toBean(bo, SysCycle.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(SysCycleEditBo bo) {
		SysCycle update = BeanUtil.toBean(bo, SysCycle.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(SysCycle entity) {
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if (isValid) {
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
